﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Mengsk.ERP.Orders;
using Mengsk.ERP.Orders.TDOrderInterfaces;
using System.Diagnostics;

namespace Mengsk.ERP.App.Orders.OrderQuery
{
    /// <summary>
    /// OrderQueryUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class OrderQueryUserControl : UserControl, IUIPlugin
    {
        public OrderQueryUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "订单查询"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.tbSQLText.Text = "select [order].* from [Order] join [OrderItem]  on [order].ERPID =[OrderItem].[ERPID] " + Environment.NewLine + " where [Order].CreateTime>='" + DateTime.Now.AddDays(-20).ToString("yyyy-MM-dd") + "' and ";
            this.dpStartTime.SelectedDate = DateTime.Now.AddDays(-20);
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
        }

        private void Image_Click(object sender, RoutedEventArgs e)
        {

        }

        private void tbDeliveryNumber_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter)
            {
                return;
            }

            string deliveryNumber = this.tbDeliveryNumber.Text.Trim();
            if (string.IsNullOrWhiteSpace(deliveryNumber))
            {
                return;
            }

            try
            {
                Order[] orders = OrderManager.Instance.GetOrdersByDeliveryNumber(deliveryNumber);
                foreach (Order order in orders)
                {
                    Order onlineOrder = TDOrderInterfaceBase.GetOrderInterfaceSource(order.Shop).DownLoadOrder(order.POPID, order.CreateTime, false);
                    order.BuyerComment = onlineOrder.BuyerComment;
                    order.SellerComment = onlineOrder.SellerComment;
                    order.Flag = onlineOrder.Flag;
                    order.State = onlineOrder.State;
                    order.Address = onlineOrder.Address;
                }
                this.orderDetailUserControl1.Orders = orders;
                this.tbDeliveryNumber.Text = "";

                if (orders.Length < 1)
                {
                    MessageBox.Show("未找到语录，请手动添加");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void UpdateOrders(Order[] orders)
        {
            int i = 0;
            Debug.WriteLine(orders.Length);
            foreach (Order order in orders)
            {
                try
                {
                    Order onlineOrder = TDOrderInterfaceBase.GetOrderInterfaceSource(order.Shop).DownLoadOrder(order.POPID, order.CreateTime, false);
                    order.BuyerComment = onlineOrder.BuyerComment;
                    order.SellerComment = onlineOrder.SellerComment;
                    order.Flag = onlineOrder.Flag;
                    order.State = onlineOrder.State;
                    order.Address = onlineOrder.Address;
                    Debug.WriteLine(++i);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            Debug.WriteLine("OK");
        }

        private void QueryBasic()
        {
            string qq = this.tbID.Text.Trim();
            string realName = this.tbRealName.Text.Trim();
            string deliveryNumber = this.tbDeliveryNumber.Text.Trim();
            string phone = this.tbPhone.Text.Trim();
            string number = this.tbNumber.Text.Trim();
            string color = this.tbColor.Text.Trim();
            string size = this.tbSize.Text.Trim();
            string productName = this.tbProductName.Text.Trim();
            string address = this.tbAddress.Text.Trim();
            string comment = this.tbComment.Text.Trim();
            DateTime date = this.dpStartTime.SelectedDate == null ? DateTime.MinValue : this.dpStartTime.SelectedDate.Value.Date;

            try
            {
                Order[] orders = OrderManager.Instance.GetOrdersByQuery(qq, realName, deliveryNumber, phone, number, color, size, date.Date, productName, address, comment);
                this.UpdateOrders(orders);
                this.orderDetailUserControl1.Orders = orders;
                if (orders.Length == 0)
                {
                    MessageBox.Show("未找到相应订单");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void QueryAdd()
        {
            try
            {
                string query = this.tbSQLText.Text.Trim();
                Order[] orders = OrderManager.Instance.GetOrdersByQuery2(query);
                this.UpdateOrders(orders);
                this.orderDetailUserControl1.Orders = orders;
                if (orders.Length == 0)
                {
                    MessageBox.Show("未找到相应订单");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            if (this.tc.SelectedIndex == 0)
            {
                this.QueryBasic();
            }
            else
            {
                this.QueryAdd();
            }
        }
    }
}
